const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 上一张 下一张 退货
router.post('/bill/showBill', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let type = body.type
    let billsId = body.billsId
    if(billsId) {
        if(type === 'next') {
            let model = await db.query(`select top 1 * from invoice_back_bak where tenantId=${user.tenantId} and id>${billsId} `,{type: QueryTypes.SELECT,plain: true})
            if(!model) return ctx.err('当天没有下一张')
            return ctx.suc('',model)
        }
        else {
            let model = await db.query(`select top 1 * from invoice_back_bak where tenantId=${user.tenantId} and id<${billsId} order by id desc`,{type: QueryTypes.SELECT,plain: true})
            if(!model) return ctx.err('当天没有上一张')
            return ctx.suc('', model)
        }
    }
    else {
        // 今天第一张saveBack
        if(type === 'next') {
            let model = await db.query(`select top 1 * from invoice_back_bak where tenantId=${user.tenantId} `,{type: QueryTypes.SELECT,plain: true})
            if(!model) return ctx.err('当天没有下一张')
            return ctx.suc('',model)
        }
        else {
            let model = await db.query(`select top 1 * from invoice_back_bak where tenantId=${user.tenantId} order by id desc `,{type: QueryTypes.SELECT,plain: true})
            if(!model) return ctx.err('当天没有上一张')
            return ctx.suc('', model)
        }
    }

})

module.exports = router;